#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
typedef int Datetype;
typedef struct stack
{
    Datetype* p;
    int top;
    int capacity;
}st;
void StInit(st* s)
{
    assert(s);
    s->capacity = s->top = 0;
    s->p = NULL;
}
void StPush(st* s, Datetype x)
{
    assert(s);
    if (s->capacity == s->top) {
        int newcapacity = s->capacity == 0 ? 4 : s->capacity * 2;
        Datetype* ptr = (Datetype*)realloc(s->p, newcapacity * sizeof(Datetype));
        if (ptr == NULL) {
            perror("realloc fail");
            return;
        }
        s->p = ptr;
        s->capacity = newcapacity;
    }
    s->p[s->top++] = x;
}
void StPop(st* s)
{
    assert(s);
    s->top--;
}
void StDestroy(st* s)
{
    assert(s);
    free(s->p);
    s->p = NULL;
    s->top = s->capacity = 0;
}
bool JudgeEmpty(st* s)
{
    if (s->top == 0) {
        return true;
    }
    else {
        return false;
    }
}
Datetype GetTop(st* s)
{
    assert(s);
    return s->p[s->top - 1];
}
int GetSize(st* s)
{
    assert(s);
    return s->top;
}
typedef struct {
    st s1;
    st s2;
} MyQueue;
MyQueue* myQueueCreate() {
    MyQueue* queue = (MyQueue*)malloc(sizeof(MyQueue));
    StInit(&queue->s1);
    StInit(&queue->s2);
    return queue;
}
void myQueuePush(MyQueue* obj, int x) {
    st* empty = &obj->s1;
    st* nonempty = &obj->s2;
    if (JudgeEmpty(&obj->s2)) {
        empty = &obj->s2;
        nonempty = &obj->s1;
    }
    StPush(nonempty, x);
}
int myQueuePop(MyQueue* obj) {
    st* empty = &obj->s1;
    st* nonempty = &obj->s2;
    if (JudgeEmpty(&obj->s2)) {
        empty = &obj->s2;
        nonempty = &obj->s1;
    }
    while (GetSize(nonempty) > 1) {
        StPush(empty, nonempty->p[nonempty->top - 1]);
        StPop(nonempty);
    }
    int val = nonempty->p[nonempty->top - 1];
    StPop(nonempty);
    while (GetSize(empty) > 0) {
        StPush(nonempty, empty->p[empty->top - 1]);
        StPop(empty);
    }
    return val;
}
int myQueuePeek(MyQueue* obj) {
    st* empty = &obj->s1;
    st* nonempty = &obj->s2;
    if (JudgeEmpty(&obj->s2)) {
        empty = &obj->s2;
        nonempty = &obj->s1;
    }
    while (GetSize(nonempty) > 1) {
        StPush(empty, nonempty->p[nonempty->top - 1]);
        StPop(nonempty);
    }
    int val = nonempty->p[nonempty->top - 1];
    while (GetSize(empty) > 0) {
        StPush(nonempty, empty->p[empty->top - 1]);
        StPop(empty);
    }
    return val;
}
bool myQueueEmpty(MyQueue* obj) {
    return JudgeEmpty(&obj->s1) && JudgeEmpty(&obj->s2);
}
void myQueueFree(MyQueue* obj) {
    StDestroy(&obj->s1);
    StDestroy(&obj->s2);
    free(obj);
}